package com.ruantong.dingtalk.ding.openapi;

import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserGetDeptMemberRequest;
import com.dingtalk.api.request.OapiUserGetOrgUserCountRequest;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserListbypageRequest;
import com.dingtalk.api.response.OapiUserGetDeptMemberResponse;
import com.dingtalk.api.response.OapiUserGetOrgUserCountResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserListbypageResponse;
import com.ruantong.dingtalk.ding.config.UserOrderType;
import com.ruantong.dingtalk.ding.openapi.token.DingTokenStore;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description:钉钉用户相关的api
 * @author: jh.chen
 * @time: 2020/11/14 10:49
 */
@Component
@Slf4j
public class UserApi {

    @Autowired
    private DingTokenStore dingTokenStore;

    public OapiUserGetResponse getUserInfoById(String userId) {
        OapiUserGetResponse response = null;
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
            OapiUserGetRequest req = new OapiUserGetRequest();
            req.setUserid(userId);
            req.setHttpMethod("GET");
            response = client.execute(req, dingTokenStore.getToken());
            log.info("UserGet rsp:{}", response);
        } catch (ApiException e) {
            log.error(e.getErrCode(), e.getErrMsg());
        }
        return response;
    }

    public List<String> getAllUserIds() {
        return getDeptMember(1);
    }

    /**
     * 获取部门下用户id
     *
     * @param: [deptId]
     * @return: java.util.List<java.lang.String>
     */
    public List<String> getDeptMember(Integer deptId) {
        List<String> userIds = null;
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getDeptMember");
            OapiUserGetDeptMemberRequest req = new OapiUserGetDeptMemberRequest();
            req.setDeptId(String.valueOf(deptId));
            req.setHttpMethod("GET");
            OapiUserGetDeptMemberResponse rsp = client.execute(req, dingTokenStore.getToken());
            userIds = rsp.getUserIds();
        } catch (ApiException e) {
            log.error(e.getErrCode(), e.getErrMsg());
        }
        return userIds;
    }

    public List<OapiUserListbypageResponse.Userlist> getAllUserInfoByDept(Long deptId, Long offset, Long size) {
        return getDeptUserInfoByPage(deptId, offset, size);
    }

    /**
     * 分页获取部门下用户详情,建议少量多次获取更新,避免长事务
     *
     * @param: [deptId]
     * @return: java.util.List<java.lang.String>
     */
    public List<OapiUserListbypageResponse.Userlist> getDeptUserInfoByPage(Long deptId, Long offset, Long size) {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/listbypage");
            OapiUserListbypageRequest req = new OapiUserListbypageRequest();
            req.setDepartmentId(deptId);
            req.setOffset(offset * size);
            req.setSize(size);
            req.setOrder(UserOrderType.MODIFY_DESC);
            req.setHttpMethod("GET");
            OapiUserListbypageResponse rsp = client.execute(req, dingTokenStore.getToken());
            log.info("UserListbypage:{}", JSON.toJSONString(rsp));
            return rsp.getUserlist();
        } catch (ApiException e) {
            log.error(e.getErrCode(), e.getErrMsg());
        }
        return null;
    }

    /**
     * 获取企业人工数量，包含未激活
     *
     * @return: Long
     */
    public Integer getUserNumber() {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_org_user_count");
            OapiUserGetOrgUserCountRequest req = new OapiUserGetOrgUserCountRequest();
            req.setOnlyActive(false);
            req.setHttpMethod("GET");
            OapiUserGetOrgUserCountResponse rsp = client.execute(req, dingTokenStore.getToken());
            log.info("GetOrgUserCount:{}", JSON.toJSONString(rsp));
            return rsp.getCount().intValue();
        } catch (ApiException e) {
            log.error(e.getErrCode(), e.getErrMsg());
        }
        return 0;
    }
}
